package com.linkage.educloud.ah.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.util.ArrayMap;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.EditText;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.adapter.ContactsAdapter;
import com.linkage.educloud.ah.bean.ClazzContact;
import com.linkage.educloud.ah.mvp.contract.IContactClazzContract;
import com.linkage.educloud.ah.mvp.presenter.ContactClazzPresenter;
import com.linkage.educloud.ah.view.BasicFragment;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * <desc>
 * Created by The Moss on 2018/11/8.
 */

public class ContactClazzFragment extends BasicFragment implements IContactClazzContract.IView{

    private static final String CLAZZID_KEY = "clazzid";
    @BindView(R.id.contacts_search_name)
    EditText mSearchEdit;
    @BindView(R.id.contact_recycler)
    XRecyclerView mRecyclerView;

    ContactsAdapter mAdapter;
    IContactClazzContract.IPresenter iPresenter;

    public static ContactClazzFragment newInstance(long clazzid) {
        ContactClazzFragment fragment = new ContactClazzFragment();
        Bundle args = new Bundle();
        args.putLong(CLAZZID_KEY, clazzid);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_contacts_clazz;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setLoadingMoreEnabled(false);
        mRecyclerView.setPullRefreshEnabled(false);
        mAdapter = new ContactsAdapter(getActivity());
        mRecyclerView.setAdapter(mAdapter);
        iPresenter = new ContactClazzPresenter(this);
        Bundle bundle = getArguments();
        long clazzid = bundle.getLong(CLAZZID_KEY);
        iPresenter.loadDatasFromLocal(clazzid);
    }

    @OnClick(R.id.contacts_search_name)
    public void onClick() {
    }

    @Override
    public void setRecyclerAdapterDatas(List<ClazzContact> contacts, ArrayMap<String, Integer> letters) {
        if (mAdapter != null) {
            mAdapter.updateDatas(contacts,letters);
        }
    }
}
